package com.webserver_v5.core;

import com.webserver_v5.annotation.Controller;
import com.webserver_v5.annotation.RequestMapping;
import com.webserver_v5.controller.UserController;
import com.webserver_v5.http.HttpServletRequest;
import com.webserver_v5.http.HttpServletResponse;

import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static DispatcherServlet instance = new DispatcherServlet();

    private static File rootDir;
    private static File staticDir;

    private DispatcherServlet() {
    }

    public static DispatcherServlet getInstance() {
        return instance;
    }

    static {
        try {
            rootDir = new File(
                    ClientHandler.class.getClassLoader()
                            .getResource(".").toURI()
            );
            staticDir = new File(rootDir, "static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void service(HttpServletRequest request, HttpServletResponse response) {
        String path = request.getRequestURI();
        File file = new File(staticDir, path);

        try {
            File dir = new File(rootDir,"com/webserver_v5/controller");
            File[] subs = dir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File f) {
                    return f.getName().endsWith(".class");
                }
            });
            for (File sub:subs){
                String className = sub.getName().replace(".class","");
                Class cls = Class.forName("com.webserver_v5.controller."+className);
                if (cls.isAnnotationPresent(Controller.class)){
                    //通过方法类对象获取其所属的类对象
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method:methods){
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            String value = rm.value();
                            if (path.equals(value)){
                                Object controller = cls.newInstance();
                                method.invoke(controller,request,response);
                                return;
                            }
                        }
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if (file.isFile()) {
                response.setContentFile(file);

                response.addHeader("Server", "BirdWebServer");
            } else {
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File(staticDir, "root/404.html");
                response.setContentFile(file);

        }
    }
}
